/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Board;
import Model.ComputerPlayer;
import Model.Die;
import Model.HumanPlayer;
import Model.Player;
import Model.Token;
import java.util.Scanner;

/**
 *
 * @author Farrar
 */
public class Game {

    private Player cmpPlayer;
    private Player humanPlayer;
    private Die gameDie;
    private Board gameBoard;
    private String gameName;
    private int cmpPlayerRoll;
    private int humanPlayerRoll;
    Scanner in = new Scanner(System.in);
    String humanAns;

    public Game(String name, String name2) {
        gameDie = new Die();
        gameBoard = new Board();
        cmpPlayer = new ComputerPlayer();
        humanPlayer = new HumanPlayer(name);
        this.gameName = name2;
        //String turn;
    }//constructor for the game

    public String getName() {
        return this.gameName;
    }//returns the name of the game

    public void startGame() {
        System.out.println("\n****************************************************************************************************");
        System.out.println("Commands: ");
        System.out.println("'r': will roll the die for you");
        System.out.println("'q': will end the game");
        System.out.println("'f': to move forward");
        System.out.println("'b': to move backward");
        System.out.println("*****************************************************************************************************\n");
    }  //prints the rules of the game

    public boolean decideGo() {
        cmpPlayerRoll = cmpPlayer.roll(gameDie);
        System.out.println(cmpPlayer.getName() + " rolled: " + cmpPlayerRoll);

        System.out.println("Type r for your deciding roll...");
        String temp = in.nextLine();

        if (temp.equalsIgnoreCase("r")) {
            humanPlayerRoll = humanPlayer.roll(gameDie);
            System.out.println(humanPlayer.getName() + " rolled: " + humanPlayerRoll);


            if (humanPlayerRoll > cmpPlayerRoll) {
                humanPlayer.setTurn(true);
                cmpPlayer.setTurn(false);
                System.out.println(humanPlayer.getName() + " rolled " + humanPlayerRoll + ", so goes first"); //tells the user who is going first
                System.out.println("\n****************************************************************************************************");
                return true;
            } else if (humanPlayerRoll == cmpPlayerRoll) {
                System.out.println("Its a tie! time for another roll off");
                return decideGo();
            } else {
                humanPlayer.setTurn(false);
                cmpPlayer.setTurn(true);
                System.out.println("COMP rolled " + cmpPlayerRoll + ", so goes first");
            }
        } else if (temp.equalsIgnoreCase("q")) {
            System.out.println("Are you sure? y/n");
            String exit = in.nextLine();
            if (exit.equalsIgnoreCase("y")) {
                System.out.println("Thankyou for playing!");
                System.exit(0);
            } else {
                return decideGo();
            }
        } else {
            System.out.println("Please type r to roll, q to quit");
            return decideGo();
        }
        return false;
    }//End method decideGo

    public int compGo(Player p1, Token t, Token hum) {
        int currentPosition = t.getPosition().getNumber();
        int computerRolled = cmpPlayer.roll(gameDie);
        System.out.println("\n****************************************************************************************************");
        System.out.println(p1.getName() + " rolled a " + computerRolled);
        boolean canMove = checkMove(computerRolled, t, p1);

        if (canMove) {
            System.out.println("\n" + p1.getName() + " is currently on square number " + currentPosition);

            t.moveCmpToken(computerRolled, "f");//always move forwards on the first roll
            currentPosition = t.getPosition().getNumber();
            System.out.println("\n" + p1.getName() + " is now on square number " + currentPosition);
            checkBump(t, hum);

            if (computerRolled == 6) {
                System.out.println("\n" + "Rolled a 6, computer will roll again");
                return compGo(p1, t, hum);
            }
        } else {
            System.out.println("Move not possible! Maybe next roll...");
        }
        return currentPosition;
    }

    //End CompGo
    public int humanGo(Player p2, Token t, Token comp) {

        int currentPosition = t.getPosition().getNumber();
        System.out.println("\nType r to roll");
        String temp = in.nextLine();
        System.out.println("\n*****************************************************************************************************");
        if (temp.equalsIgnoreCase("r")) {
            int humanRolled = humanPlayer.roll(gameDie);
            System.out.println(p2.getName() + " rolled a " + humanRolled);

            boolean canMove = checkMove(humanRolled, t, p2);
            boolean canMoveBack = false;

            if (canMove) {
                System.out.println("\n" + p2.getName() + " is currently on square number " + currentPosition);
                boolean validAns = false;
                while(!validAns) {
                System.out.println("Forwards or backwards (Type f/b)");
                String direction = in.nextLine();
                if (direction.equalsIgnoreCase("f")) {
                    t.moveCmpToken(humanRolled, "f"); break;
                } else if (direction.equalsIgnoreCase("b")) {
                    canMoveBack = MoveBack(humanRolled, t);
                
                    if (canMoveBack) {
                        t.moveCmpToken(humanRolled, "b"); break;
                    } else {
                    System.out.println("Backwards not possible!");
                    t.moveCmpToken(humanRolled, "f"); break;
                    }
                }
                else {
                    System.out.println("Not a valid input!");
                }
                }

                currentPosition = t.getPosition().getNumber();
                System.out.println(p2.getName() + " is now on square number " + currentPosition);
                checkBump(t, comp);

                if (humanRolled == 6) {
                    System.out.println("\n" + "*****Rolled a 6, so roll again*****");
                    return humanGo(p2, t, comp);
                }

            } else {
                System.out.println("\n*****************************************************************************************************");
                System.out.println("\n" + p2.getName() + " is currently on square number " + currentPosition);

                System.out.println("\nMove not possible! Would you like to move back?(type y for yes or anything else to stay put)");
                String backMove = in.nextLine();

                if(backMove.equalsIgnoreCase("y")) {
                    t.moveCmpToken(humanRolled, "b");
                }
                currentPosition = t.getPosition().getNumber();
                System.out.println("\n" + p2.getName() + " is on square number " + currentPosition);

                if (humanRolled == 6) {
                    System.out.println("\n" + "*****Rolled a 6, so roll again*****");
                    return humanGo(p2, t, comp);
                }

            }
        } else if (temp.equalsIgnoreCase("q")) {
            System.out.println("Are you sure? y/n");
            String exit = in.nextLine();
            if (exit.equalsIgnoreCase("y")) {
                System.out.println("Thankyou for playing!");
                System.exit(0);
            } else {
                return humanGo(p2, t, comp);
            }
        } else {
            System.out.println("Please type r to roll and q to quit!");
            return humanGo(p2, t, comp);
        }

        return currentPosition;
    }//End humanGo

    public boolean checkMove(int roll, Token t, Player p) {
        int totalSquares = gameBoard.getNumOfSquares();
        int possibleMove = roll + t.getPosition().getNumber();

        if (possibleMove < totalSquares) {
            return true;
        } else if (possibleMove == totalSquares) {
            System.out.println("Congratulations! " + p.getName() + ", you have won the game!!!");
            System.out.println("\n****************************************************************************************************");
            System.exit(0);
        }

        return false;

    }

    public void checkBump(Token t1, Token t2) {
        if (t1.getPosition().getNumber() == t2.getPosition().getNumber()) {
            t2.bump();
        }
    }

    public boolean MoveBack(int roll, Token t) {
        int possibleMove;
        possibleMove = t.getPosition().getNumber() - roll;
       

        if (possibleMove >= 1) {
            return true;
        } else {
            return false;
        }
    }
}//End class Game2
